import 'dart:io';

import 'package:mapbox_maps_flutter/mapbox_maps_flutter.dart';

/// 定位回调 bean
class LocationBean {
  /// 回调时间，格式为"yyyy-MM-dd HH:mm:ss"
  String? callbackTime;

  /// 定位时间， 格式为"yyyy-MM-dd HH:mm:ss"
  String? locationTime;

  ///  定位类型， 具体类型可以参考https://lbs.amap.com/api/android-location-sdk/guide/utilities/location-type
  int? locationType;

  /// 纬度
  double? latitude;

  /// 精度
  double? longitude;

  /// 精确度
  double? accuracy;

  /// 海拔, android上只有locationType==1时才会有值
  double? altitude;

  /// 角度，android上只有locationType==1时才会有值
  double? bearing;

  /// 速度， android上只有locationType==1时才会有值
  double? speed;

  /// 国家，android上只有通过[AMapLocationOption.needAddress]为true时才有可能返回值
  String? country;

  /// 省，android上只有通过[AMapLocationOption.needAddress]为true时才有可能返回值
  String? province;

  /// 城市，android上只有通过[AMapLocationOption.needAddress]为true时才有可能返回值
  String? city;

  /// 城镇（区），android上只有通过[AMapLocationOption.needAddress]为true时才有可能返回值
  String? district;

  /// 街道，android上只有通过[AMapLocationOption.needAddress]为true时才有可能返回值
  String? street;

  /// 门牌号，android上只有通过[AMapLocationOption.needAddress]为true时才有可能返回值
  String? streetNumber;

  /// 城市编码，android上只有通过[AMapLocationOption.needAddress]为true时才有可能返回值
  String? cityCode;

  /// 区域编码， android上只有通过[AMapLocationOption.needAddress]为true时才有可能返回值
  String? adCode;

  /// 地址信息， android上只有通过[AMapLocationOption.needAddress]为true时才有可能返回值
  String? address;

  /// 位置语义， android上只有通过[AMapLocationOption.needAddress]为true时才有可能返回值
  String? description;

  Position? position;

  LocationBean({
    this.callbackTime,
    this.locationTime,
    this.locationType,
    this.latitude,
    this.longitude,
    this.accuracy,
    this.altitude,
    this.bearing,
    this.speed,
    this.country,
    this.province,
    this.city,
    this.district,
    this.street,
    this.streetNumber,
    this.cityCode,
    this.adCode,
    this.address,
    this.description,
  });

  LocationBean.fromJson(dynamic json) {
    callbackTime = json['callbackTime'];
    locationTime = json['locationTime'];
    locationType = json['locationType'];
    latitude = toDouble(json["latitude"], 0.0);
    longitude = toDouble(json["longitude"], 0.0);
    accuracy = json['accuracy'];
    altitude = json['altitude'];
    bearing = json['bearing'];
    speed = json['speed'];
    country = json['country'];
    province = json['province'];
    city = json['city'];
    district = json['district'];
    street = json['street'];
    streetNumber = json['streetNumber'];
    cityCode = json['cityCode'];
    adCode = json['adCode'];
    address = json['address'];
    description = json['description'];
  }

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['callbackTime'] = callbackTime;
    map['locationTime'] = locationTime;
    map['locationType'] = locationType;
    map['latitude'] = latitude;
    map['longitude'] = longitude;
    map['accuracy'] = accuracy;
    map['altitude'] = altitude;
    map['bearing'] = bearing;
    map['speed'] = speed;
    map['country'] = country;
    map['province'] = province;
    map['city'] = city;
    map['district'] = district;
    map['street'] = street;
    map['streetNumber'] = streetNumber;
    map['cityCode'] = cityCode;
    map['adCode'] = adCode;
    map['address'] = address;
    map['description'] = description;
    return map;
  }
}

/// 定位失败时 bean
class LocationFailBean {
  /// 错误码，当定位失败时才会返回对应的错误码， 具体错误请参考：https://lbs.amap.com/api/android-location-sdk/guide/utilities/errorcode
  String? errorCode;

  /// 错误信息， 当定位失败时才会返回
  String? errorInfo;

  LocationFailBean({this.errorCode, this.errorInfo});

  LocationFailBean.fromJson(dynamic json) {
    errorCode = json['errorCode'];
    errorInfo = json['errorInfo'];
  }

  Map<String, dynamic> toJson() {
    final map = <String, dynamic>{};
    map['errorCode'] = errorCode;
    map['errorInfo'] = errorInfo;
    return map;
  }

  @override
  String toString() {
    return 'LocationFailBean{errorCode: $errorCode, errorInfo: $errorInfo}';
  }
}

double toDouble(dynamic input, double def) {
  if (Platform.isAndroid) {
    return input ?? 0.0;
  } else {
    double result = def;
    if (input == null || input == "") {
      return result;
    }
    result = double.parse(input);
    return result;
  }
}
